<?php


namespace App\Http\Controllers\Order;


use App\Http\Controllers\Controller;
use App\Models\Order\Flavor;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Validator;
/**
 * @group 口味
 *
 * 采用 restful api
 *
 * Body Parameters 表示请求的所需要的参数
 *
 * Query Parameters 表示返回参数的名称
 */
class FlavorController extends Controller
{
    /**
     * 口味查看
     *
     * @queryParam  flavor 口味信息
     * @queryParam  business_id 商家id
     *
     */
    public function index(Request $request){
        $query = Flavor::where('business_id',$request->user_id)->get()->toArray();
        return response()->json($query,200);
    }
    /**
     * 口味查看单条
     *
     * @queryParam  flavor 口味信息
     * @queryParam  business_id 商家id
     *
     */
    public function show($pk){

        $data = Flavor::find($pk);
        if($data == []){
            return response()->json(["message"=>"没有该信息"],406);
        }else{
            return response()->json($data,200);
        }
    }
    /**
     * 口味添加
     * @bodyParam  flavor required 口味信息
     */
    public function store(Request $request)
    {

        try{
            $type=$request->user_id;
            $data=$request->all();
            $data['business_id'] =$type;
            Flavor::insertGetId($data);
            return response()->json(["message"=>"添加成功"],200);
        }catch (QueryException $e){
            return response()->json(["message"=>"添加失败"],504);
        }
    }
    /**
     * 口味信息修改
     * @bodyParam  flavor required 口味信息
     *
     * 503 修改失败
     */
    public function update(Request $request, $pk){
        try {
            $dd=Flavor::find($pk);
            if ($dd ==[]){
                return response()->json(["message"=>"没有该信息"],404);
            }
            $flavor=$request->get('flavor');
            $row=Flavor::where('id',$pk)->update(["flavor"=>$flavor]);
            return response()->json(["message"=>"修改成功"],200);
        }catch (QueryException $e){
            return response()->json(["message"=>"修改失败"],503);
        }
    }
    /**
     * 口味信息删除
     *
     */
    public function destroy(Request $request,$pk){
        $data = Flavor::find($pk);
        if($data == []){
            return response()->json(["message"=>"没有该信息"],404);
        }
        $data->delete();
        return response()->json(["message"=>"删除成功"],200);
    }
}